<template>
  <div>
		<el-descriptions
			v-if="register"
			title="患者信息"
			border
			:column="4"
			label-width="100px">
			<el-descriptions-item label="患者姓名">{{register.name}}</el-descriptions-item>
			<el-descriptions-item label="年龄">{{register.age}}</el-descriptions-item>
			<el-descriptions-item label="性别">{{register.gender}}</el-descriptions-item>
			<el-descriptions-item label="手机号">{{getPhone(register.phone)}}</el-descriptions-item>
		</el-descriptions>
		<el-divider />
		<h4>医生诊断</h4>
		<el-form style="margin-top: 20px" :model="wyform" label-width="80px">
			<el-form-item label="主诉">
				<el-input
					type="textarea"
					:rows="3"
					v-model="wyform.chiefComplaint"
					placeholder="主诉(患者主要症状)"></el-input>
			</el-form-item>
			<el-form-item label="现病史">
				<el-input
					type="textarea"
					:rows="3"
					v-model="wyform.presentIllness"
					placeholder="请输入现病史"></el-input>
			</el-form-item>
			<el-form-item label="体格检查">
				<el-input
					type="textarea"
					:rows="3"
					v-model="wyform.physicalExamination"
					placeholder="请输入体格检查"></el-input>
			</el-form-item>
			<el-form-item label="辅助检查">
				<div style="width: 100%;">
					<el-space style="margin-bottom: 10px;">
						<el-button size="small" type="primary" plain @click="addItem">添加检查项</el-button>
						<el-button type="danger" size="small" plain @click="del">删除</el-button>
					</el-space>
					
					<el-table ref="tb" :data="wyform.auxiliaryExamination" border>
						<el-table-column type="selection"></el-table-column>
						<el-table-column label="编码" prop="code"></el-table-column>
						<el-table-column label="名称" prop="name"></el-table-column>
						<el-table-column label="检查部位" prop="examinationPosition"></el-table-column>
						<el-table-column label="单价" prop="price"></el-table-column>
						<el-table-column label="单位" prop="unit"></el-table-column>
					</el-table>
				</div>
			</el-form-item>
			<el-form-item label="初步诊断">
				<el-input
					type="textarea"
					:rows="3"
					v-model="wyform.diagnosis"
					placeholder="请输入初步诊断"></el-input>
			</el-form-item>
			<el-form-item label="治疗方案">
				<el-input
					type="textarea"
					:rows="3"
					v-model="wyform.treatmentPlan"
					placeholder="请输入治疗方案"></el-input>
			</el-form-item>
			<el-form-item label="医嘱">
				<el-input
					type="textarea"
					:rows="3"
					v-model="wyform.doctorAdvice"
					placeholder="请输入医嘱"></el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" @click="save">确定</el-button>
			</el-form-item>
		</el-form>
		<el-dialog
			title="检查项"
			v-model="show"
			:close-on-click-modal="false"
			width="750px">
			<medicalExaminationItemVue v-if="show" :items="wyform.auxiliaryExamination"></medicalExaminationItemVue>
		</el-dialog>
  </div>
</template>

<script setup>
import { copy } from 'copy-anything'
import { useRegisterStore } from '@/stores'
import { getPhone } from '@/util'
import { ElMessage, ElMessageBox } from 'element-plus'
import router from '@/router'
import { reactive, ref } from 'vue'
import medicalExaminationItemVue from './medicalExaminationItem'
import { post } from '@/axios'
const tb = ref()
const show = ref(false)
const registerStore = useRegisterStore()
const register = registerStore.getRegister()
if (!register) {
	ElMessage.error('请先在看诊列表中选择患者')
	router.push('/doctor/records/list')
}
const wyform = reactive({
	registerNo: register.registerNo,
	doctorId: register.doctorId,
	doctorName: register.doctorName,
	chiefComplaint: '',
	presentIllness: '',
	physicalExamination: '',
	auxiliaryExamination: [],
	treatmentPlan: '',
	diagnosis: ''
})
function addItem () {
	show.value = true
}
function del () {
	const rows = tb.value.getSelectionRows()
	if (rows.length === 0) {
		ElMessage.error('请先选择要删除的检查项')
		return
	}
	ElMessageBox.confirm('确定要删除该检查项吗?', '警告', {
		type: 'warning',
		confirmButtonText: '确定',
		cancelButtonText: '取消'
	}).then(() => { 
		for (const i in rows) {
			delItem(rows[i])
		}
	}).catch(() => {})
}
function delItem (row) {
	for (const i in wyform.auxiliaryExamination) {
		const item = wyform.auxiliaryExamination[i]
		if (item.id === row.id) {
			wyform.auxiliaryExamination.splice(i, 1)
			return
		}
	}
}
function save () {
	const form = copy(wyform)
	if (wyform.auxiliaryExamination.length === 0) {
		form.auxiliaryExamination = ''
	} else {
		let total = 0
		for (const i in wyform.auxiliaryExamination) {
			total += wyform.auxiliaryExamination[i].price
		}
		form.total = total
		form.auxiliaryExamination = JSON.stringify(wyform.auxiliaryExamination) 
	}
	post('/pmsConsultation/add', form, content => {
		console.dir(content)
	})
	
}
</script>

<style scoped lang="scss">
</style>